package levelOrder102;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LevelOrder {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resList = new ArrayList<List<Integer>>();

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()){
            int len = queue.size();

            ArrayList<Integer> temp = new ArrayList<>();
            for (int i = 0; i <len ; i++) {
                TreeNode poll = queue.poll();
                temp.add(poll.val);

                if(poll.left!=null){
                    queue.add(poll.left);
                }

                if(poll.right!=null){
                    queue.add(poll.right);
                }

            }
            resList.add(temp);
        }



        return resList;
    }


    public static void main(String[] args) {
        LevelOrder levelOrder = new LevelOrder();
        TreeNode treeNode1= new TreeNode();
        TreeNode treeNode2= new TreeNode();
        TreeNode treeNode3= new TreeNode();
        treeNode1.val=3;
        treeNode2.val=5;
        treeNode3.val=7;
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        System.out.println(levelOrder.levelOrder(treeNode1));
    }


}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}